//
//  CKXViewController.m
//  GameBaby
//
//  Created by keyZhang on 14-2-13.
//  Copyright (c) 2014年 keyZhang. All rights reserved.
//

#import "CKXViewController.h"
#import "DataService.h"
#import "CKXCellModel.h"

@interface CKXViewController ()
{
    NSMutableArray *mArray;
}

@end

@implementation CKXViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        t.font = [UIFont systemFontOfSize:16];
        t.textColor = [UIColor blackColor];
        t.backgroundColor = [UIColor clearColor];
        t.textAlignment = UITextAlignmentCenter;
        t.text = @"存卡箱";
        self.navigationItem.titleView = t;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.kcxTbView setBackgroundView:[[UIView alloc] init]];
    self.kcxTbView.backgroundView.backgroundColor = [UIColor whiteColor];
    
    __block CKXViewController *this = self;
    self.kcxTbView.finishBlock = ^{
        [this _loadMoreData];
    };
    
    mArray = [NSMutableArray array];
    
    [self loadData];
    
    self.kcxTbView.refreshDelegate = self;

}

- (void)_loadMoreData {
    mArray = [NSMutableArray array];
    [self loadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *url = [NSString stringWithFormat:@"http://m.a.ewoka.com/apply/personal/mylika/%d",mArray.count/10+1];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[[NSUserDefaults standardUserDefaults] objectForKey:@"appid"],@"appid",nil];
    __weak CKXViewController *this = self;
    [DataService requestWithURL:url
                         params:dic
                     httpMethod:@"POST"
                    finishBlock:^(id result) {
                        [this loadDataFinished:result];
                    }];
}

- (void)loadDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"list"];
    if (array.count < 10) {
        self.kcxTbView.isMore = NO;
    }else
    {
        self.kcxTbView.isMore = YES;
    }
    for (NSDictionary *dic in array) {
        CKXCellModel *model = [[CKXCellModel alloc] initContentWith:dic];
        [mArray addObject:model];
    }
    self.kcxTbView.dataList = mArray;
    [self.kcxTbView reloadData];
    [self.kcxTbView doneLoadingTableViewData];
}

#pragma mark - BaseTableView delegate
//上拉加载的协议方法
- (void)pullUp:(BaseTableView *)tableView {
    NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
    if (![str isEqualToString:@"加载更多数据"]) {
        return;
    }else
    {
        [self loadData];
    }
}

@end
